/**
 * 
 */
package com.techq.weibo.robot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import com.techq.weibo.auth.Auth;
import com.techq.weibo.meta.User;
import com.techq.weibo.share.enums.DestShare;
import com.techq.weibo.share.enums.Target;
import com.techq.weibo.share.exception.ShareException;

/**
 * @author tassemble@gmail.com
 * 2011-8-2
 */
public abstract class AbstractCrawlRobot implements CrawlRobot {
	
	Auth auth;
	protected HttpClient httpClient;
	Logger logger = Logger.getLogger(AbstractCrawlRobot.class);
	String lastPage;
	/**
	 * 
	 */
	
	public AbstractCrawlRobot() {
		httpClient = new DefaultHttpClient();
		HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);  
	}
	
	/* (non-Javadoc)
	 * @see com.techq.weibo.share.CrawlRobot#get(org.apache.http.client.HttpClient, java.lang.String)
	 */
	
	@Override
	public StringBuilder get(String url) throws ShareException {
		HttpGet get = new HttpGet(url);
		HttpResponse response = null;
		StringBuilder result;
		try {
			response = httpClient.execute(get);
			HttpEntity entity = response.getEntity();
			result = getContent(entity);
			this.setLastPage(result.toString());
			//System.out.println(result);
		} catch (ClientProtocolException e) {
			e.printStackTrace(System.out);
			throw new ShareException("http get ClientProtocolException exception", e);
		} catch (IOException e) {
			e.printStackTrace(System.out);
			throw new ShareException("http get IOException exception", e);
		}
		return result;
	}
	
	private StringBuilder getContent(HttpEntity entity) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(
				entity.getContent(), "UTF-8"));
		
		StringBuilder sb = new StringBuilder(1024 * 8);
		String line;
		while((line = br.readLine()) != null) {
			sb.append(line + "\n\t");
		}
		return sb;
	}

	/* (non-Javadoc)
	 * @see com.techq.weibo.share.CrawlRobot#post(org.apache.http.client.HttpClient, java.lang.String, java.util.List)
	 */
	@Override
	public String post(String url, List<NameValuePair> params) throws ShareException {
		throw new ShareException("not implememted");
	}
	
	
	@Override
	public abstract boolean auth(String url, User user) throws ShareException;

	@Override
	public abstract String lookUp(Target target, String url, DestShare dest)
			throws ShareException;
	
	@Override
	public StringBuilder execute(HttpUriRequest request) throws ShareException {
		
		try {
			HttpResponse response = httpClient.execute(request);
			HttpEntity entity = response.getEntity();
			request.abort();
			return getContent(entity);
		} catch (ClientProtocolException e) {
			logger.error("ClientProtocolException", e);
			throw new ShareException("ClientProtocolException", e);
		} catch (IOException e) {
			logger.error("IOException", e);
			throw new ShareException("IOException", e);
		}
	}

	
	@Override
	public void setLastPage(String page) throws ShareException {
		this.lastPage = new String(page);
	}
	
	@Override
	public String getLastPage() throws ShareException {
		return this.lastPage;
	}

}
